package com.lu.spzx.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lu.spzx.manager.mapper.ProductDetailsMapper;
import com.lu.spzx.manager.mapper.ProductMapper;
import com.lu.spzx.manager.mapper.ProductSkuMapper;
import com.lu.spzx.manager.service.ProductService;
import com.lu.spzx.model.dto.product.ProductDto;
import com.lu.spzx.model.entity.product.Product;
import com.lu.spzx.model.entity.product.ProductDetails;
import com.lu.spzx.model.entity.product.ProductSku;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    @Override
    public PageInfo<Product> findByPage(Integer page, Integer limit, ProductDto productDto) {
        PageHelper.startPage(page, limit);
        List<Product> productList = this.productMapper.findByPage(productDto);
        return new PageInfo<>(productList);
    }

    /*
        @Transactional 默认情况下只拦截运行时间异常 @RuntimeException
        但是 IOException SQLException 不在 RuntimeException 体系下
        所以需要扩大异常的拦截范围
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Product product) {
        // 保存商品数据
        // 设置上架状态为 0
        product.setStatus(0);
        // 设置审核状态为 0
        product.setAuditStatus(0);
        this.productMapper.save(product);

        // 保存商品详情数据
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        this.productDetailsMapper.save(productDetails);

        // 保存商品 sku 数据
        List<ProductSku> productSkuList = product.getProductSkuList();
        for (int i = 0, size = productSkuList.size(); i < size; i++) {
            ProductSku productSku = productSkuList.get(i);
            // 构建 skuCode
            productSku.setSkuCode(product.getId() + "_" + i);
            // 设置商品 id
            productSku.setProductId(product.getId());
            productSku.setSkuName(product.getName() + productSku.getSkuSpec());
            // 设置销量
            productSku.setSaleNum(0);
            productSku.setStatus(0);
            this.productSkuMapper.save(productSku);
        }
    }

    @Override
    public Product getById(Long id) {
        // 根据 id 查询商品数据
        Product product = this.productMapper.selectById(id);

        // 根据商品的id查询sku数据
        List<ProductSku> productSkuList = this.productSkuMapper.selectByProductId(id);
        product.setProductSkuList(productSkuList);

        // 根据商品的id查询商品详情数据
        ProductDetails productDetails = this.productDetailsMapper.selectByProductId(product.getId());
        product.setDetailsImageUrls(productDetails.getImageUrls());

        // 返回数据
        return product;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateById(Product product) {
        // 修改商品基本数据
        this.productMapper.updateById(product);

        // 修改商品的sku数据
        List<ProductSku> productSkuList = product.getProductSkuList();
        productSkuList.forEach(productSku -> {
            this.productSkuMapper.updateById(productSku);
        });

        // 修改商品的详情数据
        ProductDetails productDetails = productDetailsMapper.selectByProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        this.productDetailsMapper.updateById(productDetails);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(Long id) {
        // 根据 id 删除商品基本数据
        this.productMapper.deleteById(id);
        // 根据商品 id 删除商品的 sku 数据
        this.productSkuMapper.deleteByProductId(id);
        // 根据商品的 id 删除商品的详情数据
        this.productDetailsMapper.deleteByProductId(id);
    }

    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        Product product = new Product();
        product.setId(id);
        if (auditStatus == 1) {
            product.setAuditStatus(1);
            product.setAuditMessage("审批通过");
        } else {
            product.setAuditStatus(-1);
            product.setAuditMessage("审批不通过");
        }
        this.productMapper.updateById(product);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(Long id, Integer status) {
        Product product = new Product();
        product.setId(id);
        if (status == 1) {
            product.setStatus(1);
        } else {
            product.setStatus(-1);
        }
        productMapper.updateById(product);
    }

}